package com.ss.android.common.util.json;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public final class JsonUtil {
    static final class ClassInfo {
        List<MyField> seriFields;
        Method contructorMethod;
        int mask;
        public static final ClassInfo NOT_USE_CACHE = new ClassInfo();

        private ClassInfo() {

        }
    }

    static final class MyField {
        public Field realField;
        public String keyName;
        public Class c;
        public Class d;

        private MyField() {

        }
    }

    private static HashMap<Class<?>,ClassInfo> sCacheSerializableFields = new HashMap();
    private static IJsonInstanceFactory sJsonInstanceFactory;

    public static void setJsonInstanceFactory(IJsonInstanceFactory factory) {
        JsonUtil.sJsonInstanceFactory = factory;
    }

    public static String a(String arg1, String arg2, String arg3) {
        if (!android.support.design.a.isTextEmpty(arg1) && !android.support.design.a.isTextEmpty(arg2) && !android.support.design.a.isTextEmpty(arg3)) {
            try {
                JSONObject v0_1 = new JSONObject(arg1);
                v0_1.put(arg2, arg3);
                arg1 = v0_1.toString();
            } catch (Exception v0) {
                v0.printStackTrace();
            }
        }

        return arg1;
    }

    public static void parseJSONObject(JSONObject jo, Object receiver) {
        if (jo != null && receiver != null) {
            Object v2 = null;
            Object v3 = null;
            Object v4 = JsonUtil.a(receiver.getClass(), JsonSerializable.class) ? receiver : v2;
            Object v5 = JsonUtil.a(receiver.getClass(), JsonParseListener.class) ? receiver : v3;
            List v8 = JsonUtil.b(receiver.getClass());
            if (v8 != null && v8.size() != 0) {
                int v9 = v8.size();
                int v7 = 0;
                while (true) {
                    if (v7 < v9) {
                        v2 = v8.get(v7);
                        String v3_1 = ((MyField) v2).keyName;
                        if ((jo.has(v3_1)) && v3_1 != null && v3_1.length() != 0) {
                            if (v4 != null) {
                                if (!((JsonSerializable) v4).parseSpecialField(v3_1, jo)) {
                                } else {
                                    ++v7;
                                    continue;
                                }
                            }
                            try {
                                Class v10 = ((MyField) v2).c;
                                if (v10.isArray()) {
                                    JSONArray v6 = jo.optJSONArray(v3_1);
                                    Class v11 = v10.getComponentType();
                                    int v12 = v6.length();
                                    ArrayList v13 = new ArrayList();
                                    int v3_2;
                                    for (v3_2 = 0; v3_2 < v12; ++v3_2) {
                                        if (v6.opt(v3_2) != null) {
                                            Object v14 = JsonUtil.a(v11, v6, v3_2);
                                            if (v14 != null) {
                                                v13.add(v14);
                                            }
                                        }
                                    }

                                    if (v13.size() == 0) {
                                        v3 = null;
                                    } else {
                                        v3 = Array.newInstance(v11, v13.size());
                                        int v6_1;
                                        for (v6_1 = 0; v6_1 < v13.size(); ++v6_1) {
                                            Array.set(v3, v6_1, v13.get(v6_1));
                                        }
                                    }
                                } else {
                                    if (JsonUtil.a(v10, List.class)) {
                                        v3 = JsonUtil.extractListFromJson(jo.optJSONArray(v3_1), v10, ((MyField) v2));
                                    } else {
                                        v3 = JsonUtil.a(v3_1, v10, jo);
                                    }
                                }

                                if (v3 != null) {
                                    Field v6_2 = ((MyField) v2).realField;
                                    if (v10 != Integer.TYPE && v10 != Integer.class) {
                                        if (v10 != Long.TYPE && v10 != Long.class) {
                                            if (v10 != Float.TYPE && v10 != Float.class) {
                                                if (v10 != Double.TYPE && v10 != Double.class) {
                                                    if (v10 != Boolean.TYPE && v10 != Boolean.class) {
                                                        if (v10 != Character.TYPE && v10 != Character.class) {
                                                            if (v10 != Short.TYPE && v10 != Short.class) {
                                                                if (v10 != Byte.TYPE && v10 != Byte.class) {
                                                                    v6_2.set(receiver, v3);
                                                                    ++v7;
                                                                    continue;
                                                                }

                                                                v6_2.setByte(receiver, ((Byte) v3).byteValue());
                                                                ++v7;
                                                                continue;
                                                            }

                                                            v6_2.setShort(receiver, ((Short) v3).shortValue());
                                                            ++v7;
                                                            continue;
                                                        }

                                                        v6_2.setChar(receiver, ((Character) v3).charValue());
                                                        ++v7;
                                                        continue;
                                                    }

                                                    v6_2.setBoolean(receiver, ((Boolean) v3).booleanValue());
                                                    ++v7;
                                                    continue;
                                                }

                                                v6_2.setDouble(receiver, ((Double) v3).doubleValue());
                                                ++v7;
                                                continue;
                                            }

                                            v6_2.setFloat(receiver, ((Float) v3).floatValue());
                                            ++v7;
                                            continue;
                                        }
                                        v6_2.setLong(receiver, ((Long) v3).longValue());
                                        ++v7;
                                        continue;
                                    }
                                    v6_2.setInt(receiver, ((Integer) v3).intValue());
                                }
                                ++v7;
                                continue;
                            } catch (Exception v2_1) {
                                throw new RuntimeException(((Throwable) v2_1));
                            }
                        }
                        ++v7;
                        continue;
                    } else {
                        break;
                    }
                }

                if (v5 == null) {
                    return;
                }

                ((JsonParseListener) v5).onFinishParse(jo);
                return;
            }

            if (v5 == null) {
                return;
            }

            ((JsonParseListener) v5).onFinishParse(jo);
        }
    }

    public static <T> T extractObjectFromJson(JSONObject jo, Class<T> clazz) {
        Method v1_1;
        ClassInfo v2;
        T v0 = null;
        if (jo != null && clazz != null) {
            try {
                v2 = JsonUtil.a(clazz);
                if ((v2.mask & 2) != 0) {
                    v1_1 = v2.contructorMethod;
                } else {
                    try {
                        v1_1 = clazz.getMethod("createInstanceForJson", JSONObject.class);
                    } catch (Exception v1) {
                        v1_1 = ((Method) v0);
                    }

                    if (v1_1 != null) {
                        int v3 = v1_1.getModifiers();
                        if (!JsonUtil.a(v1_1.getReturnType(), clazz)) {
                            v1_1 = ((Method) v0);
                        } else if (!Modifier.isPublic(v3)) {
                            v1_1 = ((Method) v0);
                        } else if (!Modifier.isStatic(v3)) {
                            v1_1 = ((Method) v0);
                        }
                    } else {
                        v1_1 = ((Method) v0);
                    }

                    if (v2 == ClassInfo.NOT_USE_CACHE) {

                    } else {
                        synchronized (v2) {
                            v2.contructorMethod = v1_1;
                            v2.mask |= 2;
                        }
                    }
                }
                if (v1_1 == null) {
                    if (JsonUtil.sJsonInstanceFactory != null) {
                        v0 = (T) JsonUtil.sJsonInstanceFactory.newInstance(clazz, jo);
                    }
                    if (v0 != null) {
                    } else {
                        v0 = clazz.newInstance();
                    }
                } else {
                    v0 = (T) v1_1.invoke(null, jo);
                }
                JsonUtil.parseJSONObject(jo, v0);
                return v0;
            } catch (Exception v0_1) {
                label_44:
                throw new RuntimeException(((Throwable) v0_1));
            }
        }

        return v0;
    }

    public static List a(JSONArray arg4, Class arg5) {
        List v0_1;
        if (arg4 == null || arg4.length() == 0) {
            v0_1 = null;
        } else {
            int v2 = arg4.length();
            v0_1 = new ArrayList(v2);
            int v1;
            for (v1 = 0; v1 < v2; ++v1) {
                Object v3 = JsonUtil.a(arg5, arg4, v1);
                if (v3 != null) {
                    v0_1.add(v3);
                }
            }
        }

        return v0_1;
    }

    public static String a(Object arg1) {
        Object v0 = JsonUtil.b(arg1);
        String v0_1 = v0 != null ? v0.toString() : null;
        return v0_1;
    }

    private static ClassInfo a(Class arg3) {
        ClassInfo v0_2;
        Object v0 = JsonUtil.sCacheSerializableFields.get(arg3);
        if (v0 == null) {
            HashMap v1 = JsonUtil.sCacheSerializableFields;
            synchronized (v1) {
                v0_2 = !arg3.isAnnotationPresent(CacheMember.class) ? ClassInfo.NOT_USE_CACHE : new ClassInfo();
                JsonUtil.sCacheSerializableFields.put(arg3, v0_2);
                return v0_2;
            }
        } else {
            v0_2 = (ClassInfo) v0;
        }

        return v0_2;
    }

    private static Object a(Class arg2, JSONArray arg3, int arg4) {
        Object v0_3;
        if (arg2 == Integer.TYPE || arg2 == Integer.class) {
            return Integer.valueOf(arg3.optInt(arg4));
        } else {
            if (arg2 != Long.TYPE && arg2 != Long.class) {
                if (arg2 != Float.TYPE && arg2 != Float.class) {
                    if (arg2 != Double.TYPE && arg2 != Double.class) {
                        if (arg2 != Boolean.TYPE && arg2 != Boolean.class) {
                            if (arg2 != Character.TYPE && arg2 != Character.class) {
                                if (arg2 != Short.TYPE && arg2 != Short.class) {
                                    if (arg2 != Byte.TYPE && arg2 != Byte.class) {
                                        if (arg2 == String.class) {
                                            return arg3.optString(arg4);
                                        } else if (arg2 == JSONObject.class) {
                                            return arg3.optJSONObject(arg4);
                                        } else if (arg2 == JSONArray.class) {
                                            return arg3.optJSONArray(arg4);
                                        } else {
                                            if (!arg2.isArray() && !JsonUtil.a(arg2, List.class)) {
                                                v0_3 = JsonUtil.extractObjectFromJson(arg3.optJSONObject(arg4), arg2);
                                                return v0_3;
                                            }

                                            throw new RuntimeException("Not support array or List with level >1 ! (即不支持多维数组)");
                                        }
                                    }

                                    return Byte.valueOf(((byte) arg3.optInt(arg4)));
                                }

                                return Short.valueOf(((short) arg3.optInt(arg4)));
                            }

                            return Character.valueOf(((char) arg3.optInt(arg4)));
                        }

                        return Boolean.valueOf(JsonUtil.a(arg3, arg4));
                    }

                    return Double.valueOf(arg3.optDouble(arg4));
                }

                return Float.valueOf(((float) arg3.optDouble(arg4)));
            }

            return Long.valueOf(arg3.optLong(arg4));
        }
    }

    private static boolean a(Class arg7, Class arg8) {
        int v2;
        boolean v0 = false;
        if (arg7 != null && arg8 != null) {
            if (arg7 != arg8) {
                if (arg7 == null || arg8 == null) {
                    v2 = 0;
                } else {
                    Class[] v3 = arg7.getInterfaces();
                    if (v3 != null && v3.length > 0) {
                        int v4 = v3.length;
                        v2 = 0;
                        while (v2 < v4) {
                            if (v3[v2] == arg8) {
                                v2 = 1;
                                if (v2 == 0) {
                                    return v0;
                                }
                                return true;
                            } else {
                                ++v2;
                                continue;
                            }
                        }
                    }

                    Class v2_1;
                    for (v2_1 = arg7.getSuperclass(); v2_1 != null; v2_1 = v2_1.getSuperclass()) {
                        if (v2_1 == arg8) {
                            v2 = 1;
                            if (v2 == 0) {
                                return v0;
                            }
                            return true;
                        }

                        Class[] v4_1 = v2_1.getInterfaces();
                        if (v4_1 != null && v4_1.length > 0) {
                            int v5 = v4_1.length;
                            int v3_1 = 0;
                            while (v3_1 < v5) {
                                if (v4_1[v3_1] == arg8) {
                                    v2 = 1;
                                    if (v2 == 0) {
                                        return v0;
                                    }
                                    return true;
                                } else {
                                    ++v3_1;
                                    continue;
                                }
                            }
                        }
                    }

                    v2 = 0;
                }
                if (v2 == 0) {
                    return v0;
                }
                return true;
            }
            v0 = true;
        }

        return v0;
    }

    private static boolean a(JSONArray arg3, int arg4) {
        try {
            Object v0_1 = arg3.opt(arg4);
            if ((v0_1 instanceof Boolean)) {
                boolean v0_2 = ((Boolean) v0_1).booleanValue();
                return v0_2;
            }

            if (arg3.optInt(arg4, -1) <= 0) {
                return false;
            }
        } catch (Exception v0) {
            v0.printStackTrace();
            return false;
        }
        return true;

    }

    private static Object a(String arg2, Class arg3, JSONObject arg4) {
        String v0_1;
        try {
            if (arg3 == Integer.TYPE || arg3 == Integer.class) {
                return Integer.valueOf(arg4.optInt(arg2, 0));
            } else {
                if (arg3 != Long.TYPE && arg3 != Long.class) {
                    if (arg3 != Float.TYPE && arg3 != Float.class) {
                        if (arg3 != Double.TYPE && arg3 != Double.class) {
                            if (arg3 != Boolean.TYPE && arg3 != Boolean.class) {
                                if (arg3 != Character.TYPE && arg3 != Character.class) {
                                    if (arg3 != Short.TYPE && arg3 != Short.class) {
                                        if (arg3 != Byte.TYPE && arg3 != Byte.class) {
                                            if (arg3 == String.class) {
                                                return arg4.optString(arg2);
                                            } else if (arg3 == JSONObject.class) {
                                                return arg4.optJSONObject(arg2);
                                            } else if (arg3 == JSONArray.class) {
                                                return arg4.optJSONArray(arg2);
                                            } else {
                                                return JsonUtil.extractObjectFromJson(arg4.optJSONObject(arg2), arg3);
                                            }
                                        }

                                        return Byte.valueOf(((byte) arg4.optInt(arg2)));
                                    }

                                    return Short.valueOf(((short) arg4.optInt(arg2)));
                                }
                                return Character.valueOf(((char) arg4.optInt(arg2)));
                            }
                            return Boolean.valueOf(JsonUtil.a(arg2, arg4));
                        }

                        return Double.valueOf(arg4.optDouble(arg2, 0));
                    }

                    return Float.valueOf(((float) arg4.optDouble(arg2, 0)));
                }

                return Long.valueOf(arg4.optLong(arg2, 0));
            }
        } catch (Exception v0) {
            throw new RuntimeException(((Throwable) v0));
        }
    }

    private static boolean a(String arg3, JSONObject arg4) {
        try {
            Object v0_1 = arg4.opt(arg3);
            if ((v0_1 instanceof Boolean)) {
                boolean v0_2 = ((Boolean) v0_1).booleanValue();
                return v0_2;
            }

            if (arg4.optInt(arg3, -1) <= 0) {
                return false;
            }
        } catch (Exception v0) {
            v0.printStackTrace();
            return false;
        }

        return true;

    }

    private static Object extractListFromJson(JSONArray ja, Class<?> clazz, MyField myField) {
        Object v2_1;
        int v3 = 0;
        if (Modifier.isAbstract(clazz.getModifiers())) {
            v2_1 = new ArrayList();
        } else {
            try {
                v2_1 = clazz.newInstance();
            } catch (IllegalAccessException v0) {
                throw new RuntimeException("You must provide a public access Contructor without params", ((Throwable) v0));
            } catch (InstantiationException v0_1) {
                throw new RuntimeException("You must provide a public access Contructor without params", ((Throwable) v0_1));
            }
        }

        Type v1 = myField.d;
        if (v1 == null) {
            Type v0_2 = myField.realField.getGenericType();
            if ((v0_2 instanceof ParameterizedType)) {
                Type[] v0_3 = ((ParameterizedType) v0_2).getActualTypeArguments();
                if (v0_3.length > 0) {
                    v1 = v0_3[0];
                }
            }
        }

        if ((!(v1 instanceof Class) || !JsonUtil.a((Class) v1, List.class))
                && (!(v1 instanceof ParameterizedType)
                && !(v1 instanceof GenericArrayType)
                && !(v1 instanceof TypeVariable)
                && !(v1 instanceof WildcardType))) {
            if (v1 != null) {
                if (!(v1 instanceof Class)) {
                    return null;
                }

                while (true) {
                    if (v3 >= ja.length()) {
                        break;
                    }

                    if (ja.opt(v3) != null) {
                        Object v0_4 = JsonUtil.a((Class) v1, ja, v3);
                        if (v0_4 != null) {
                            ((List) v2_1).add(v0_4);
                        }
                    }
                    ++v3;
                }
            }
            if (v2_1 != null && ((List) v2_1).size() > 0) {
                return v2_1;
            }
            return null;
        }

        throw new RuntimeException("field :" + myField.realField.getName()
                + ", type:" + myField.realField.getGenericType()
                + ", the type is too complex, Please parse it yourself with override method of JsonSerializable#parseSpecialField()");
    }

    public static String a(String arg2, String arg3) {
        String v0 = "";
        if (!android.support.design.a.isTextEmpty(arg2) && !android.support.design.a.isTextEmpty(arg3)) {
            try {
                v0 = new JSONObject(arg2).optString(arg3);
            } catch (Exception v1) {
                v1.printStackTrace();
            }
        } else {
            v0 = "";
        }

        return v0;
    }

    public static Object b(Object arg4) {
        JSONObject v1_1;
        JSONArray v4_1;
        Object v3;
        int v2_1;
        JSONArray v0_1;
        String v4;
        int v0 = 1;
        int v1 = 0;
        if (arg4 == null) {
            return "";
        } else {
            Class v2 = arg4.getClass();
            if (JsonUtil.a(v2, CharSequence.class)) {
                return arg4.toString();
            } else {
                if (v2 == null || !v2.isPrimitive()
                        && v2 != Long.class
                        && v2 != Integer.class
                        && v2 != Float.class
                        && v2 != Double.class
                        && v2 != Character.class
                        && v2 != Boolean.class
                        && v2 != Short.class
                        && v2 != Byte.class) {
                    v0 = 0;
                }

                if (v0 != 0) {
                    return "";
                }

                if (v2.isArray()) {
                    v0_1 = new JSONArray();
                    v2_1 = Array.getLength(arg4);
                    while (v1 < v2_1) {
                        v3 = JsonUtil.b(Array.get(arg4, v1));
                        if (v3 != null) {
                            v0_1.put(v3);
                        }

                        ++v1;
                    }
                    return v0_1;
                }

                if (JsonUtil.a(v2, List.class)) {
                    v0_1 = new JSONArray();
                    v2_1 = ((List) arg4).size();
                    while (v1 < v2_1) {
                        v3 = JsonUtil.b(((List) arg4).get(v1));
                        if (v3 != null) {
                            v0_1.put(v3);
                        }

                        ++v1;
                    }

                    return v0_1;
                }

                if (v2 == JSONObject.class) {
                    return "";
                }

                if (v2 == JSONArray.class) {
                    return "";
                }

                try {
                    List v0_3 = JsonUtil.b(v2);
                    JsonUtil.a(v2, JsonSerializable.class);
                    v1_1 = new JSONObject();
                    if (v0_3 != null && v0_3.size() != 0) {
                        Iterator v2_2 = v0_3.iterator();
                        while (true) {
                            if (v2_2.hasNext()) {
                                Object v0_4 = v2_2.next();
                                String v3_1 = ((MyField) v0_4).keyName;
                                v0_4 = ((MyField) v0_4).realField.get(arg4);
                                if (v0_4 == null) {
                                    continue;
                                }

                                v1_1.put(v3_1, JsonUtil.b(v0_4));
                                continue;
                            } else {
                                return v1_1;
                            }
                        }
                    }
                    return v1_1;
                } catch (Exception v0_2) {
                    throw new RuntimeException(v0_2);
                }
            }
        }
    }

    private static List b(Class arg3) {
        List v0;
        ClassInfo v1 = JsonUtil.a(arg3);
        if (v1 == ClassInfo.NOT_USE_CACHE) {
            v0 = JsonUtil.c(arg3);
        } else {
            v0 = v1.seriFields;
            if ((v1.mask & 1) <= 0) {
                v0 = JsonUtil.c(arg3);
                synchronized (v1) {
                    v1.seriFields = v0;
                    v1.mask |= 1;
                }
            }
        }

        return v0;
    }

    private static List c(Class arg8) {
        ArrayList v3 = new ArrayList();
        while (arg8 != null) {
            if (arg8 == Object.class) {
                break;
            }

            Field[] v4 = arg8.getDeclaredFields();
            if (v4 != null && v4.length > 0) {
                int v5 = v4.length;
                int v1;
                for (v1 = 0; v1 < v5; ++v1) {
                    Field v6 = v4[v1];
                    if ((v6.isAnnotationPresent(KeyName.class)) && !Modifier.isStatic(v6.getModifiers()) && !v6.isEnumConstant()) {
                        v6.setAccessible(true);
                        MyField v7 = new MyField();
                        v7.realField = v6;
                        if (v6.isAnnotationPresent(KeyName.class)) {
                            v7.keyName = v6.getAnnotation(KeyName.class).value();
                        }

                        v7.c = v6.isAnnotationPresent(KeyType.class) ? v6.getAnnotation(KeyType.class).value() : v6.getType();
                        if (v6.isAnnotationPresent(ListItemType.class)) {
                            v7.d = v6.getAnnotation(ListItemType.class).value();
                        }

                        v3.add(v7);
                    }
                }
            }

            arg8 = arg8.getSuperclass();
        }

        return ((List) v3);
    }
}

